//
//  MyCaseViewController.m
//  e律师
//
//  Created by nykj-mac-03 on 2017/6/7.
//  Copyright © 2017年 test. All rights reserved.
//

#import "MyCaseViewController.h"

@interface MyCaseViewController ()<LynnTypeButtonActionDelegate,UITableViewDelegate,UITableViewDataSource>


@property (nonatomic, strong) UITableView *myCaseTablewView;
@property (nonatomic, strong) NSMutableArray *advanceData;
@property (nonatomic, strong) NSMutableArray *approvalData;
@property (nonatomic, strong) NSMutableArray *handlingData;
@property (nonatomic, strong) NSMutableArray *closedCaseData;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSDictionary *case_State;


@end

@implementation MyCaseViewController
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.advanceData = [NSMutableArray array];
    self.approvalData = [NSMutableArray array];
    self.handlingData = [NSMutableArray array];
    self.closedCaseData = [NSMutableArray array];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [self initUI];
    [self getData];
}
#pragma mark -- Method
- (void)initUI{
    LynnTypeButtonView  *typeBtnView = [[LynnTypeButtonView  alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 44)];
    [typeBtnView setTypeButtonTitles:@[@"预收案",@"审批中",@"办案中",@"已结案"] withDownLableHeight:2 andDeleagte:self];
    [typeBtnView setSelectButtonIndex:self.caseStateFlag];
    [typeBtnView setTypeButtonNormalColor:[UIColor blackColor] andSelectColor:[UIColor colorWithRed:33.0f/255.0f green:180.0f/255.0f blue:237.0f/255.0f alpha:1.0f]];
    typeBtnView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:typeBtnView];
    [self.view addSubview:self.myCaseTablewView];
    
}
- (void)getData{
    
    if ([self.caseStyle isEqualToString:@"我的案件"]) {
        self.barTitleLable.text = @"我的案件";
        [NetworkingManager getUrl:[NSString stringWithFormat:@"%@create_id=%@",MYCASE_URL,self.create_id] dic:nil Succeed:^(id responseData) {
            NSLog(@"responseData = %@",responseData);
            for (NSDictionary *dic  in responseData[@"result"]) {
                if ([dic[@"case_state"] integerValue] == 1 || [dic[@"case_state"] integerValue] == -1) {
                    [self.advanceData addObject:dic];
                    
                }
                if ([dic[@"case_state"] integerValue] == 2 || [dic[@"case_state"] integerValue] == 3 || [dic[@"case_state"] integerValue] == 5 || [dic[@"case_state"] integerValue] == 6) {
                    [self.approvalData addObject:dic];
                }
                if ([dic[@"case_state"] integerValue] == -4 || [dic[@"case_state"] integerValue] == 4) {
                    [self.handlingData addObject:dic];
                }
                if ([dic[@"case_state"] integerValue] == 7) {
                    [self.closedCaseData addObject:dic];
                }
            }
            if (self.caseStateFlag == 0) {
                self.dataSource = self.advanceData;
            }
            if (self.caseStateFlag == 1) {
                self.dataSource = self.approvalData;
            }
            if (self.caseStateFlag == 2) {
                self.dataSource = self.handlingData;
            }
            if (self.caseStateFlag == 3) {
                self.dataSource = self.closedCaseData;
            }
            if (self.dataSource.count == 0) {
                [MBProgressHUD showSuccess:@"暂无更多数据"];
            }
            [self.myCaseTablewView reloadData];
            NSLog(@"myCaseData = %@",self.dataSource);
            [self.myCaseTablewView.mj_header endRefreshing];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
            
        } failure:^(NSError *error) {
            [MBProgressHUD showError:@"网络异常"];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }else if ([self.caseStyle isEqualToString:@"律所案件"]){
        self.barTitleLable.text = @"律所案件";
        [NetworkingManager getUrl:[NSString stringWithFormat:@"%@company_id=%@",LAWOFFICECASE_URL,[InfoSingleTon personInfoSingleTon].infoDic[@"cid"]] dic:nil Succeed:^(id responseData) {
            NSLog(@"responseData = %@",responseData);
            for (NSDictionary *dic  in responseData[@"result"]) {
                if ([dic[@"case_state"] integerValue] == 1 || [dic[@"case_state"] integerValue] == -1) {
                    [self.advanceData addObject:dic];
                    
                }
                if ([dic[@"case_state"] integerValue] == 2 || [dic[@"case_state"] integerValue] == 3 || [dic[@"case_state"] integerValue] == 5 || [dic[@"case_state"] integerValue] == 6) {
                    [self.approvalData addObject:dic];
                }
                if ([dic[@"case_state"] integerValue] == -4 || [dic[@"case_state"] integerValue] == 4) {
                    [self.handlingData addObject:dic];
                }
                if ([dic[@"case_state"] integerValue] == 7) {
                    [self.closedCaseData addObject:dic];
                }
            }
            NSLog(@"预收案： %@",self.advanceData);
            if (self.caseStateFlag == 0) {
                self.dataSource = self.advanceData;
            }
            if (self.caseStateFlag == 1) {
                self.dataSource = self.approvalData;
            }
            if (self.caseStateFlag == 2) {
                self.dataSource = self.handlingData;
            }
            if (self.caseStateFlag == 3) {
                self.dataSource = self.closedCaseData;
            }
            if (self.dataSource.count == 0) {
                [MBProgressHUD showSuccess:@"暂无更多数据"];
            }
            [self.myCaseTablewView reloadData];
            [self.myCaseTablewView.mj_header endRefreshing];
            NSLog(@"myCaseData = %@",self.dataSource);
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            
        } failure:^(NSError *error) {
            [MBProgressHUD showError:@"网络异常"];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [self.myCaseTablewView.mj_header endRefreshing];
        }];
    }
}
- (void)loadNewData{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //[self.allInfoList removeObjectAtIndex:0];
//        self.isRefresh = YES;
        [self.dataSource removeAllObjects];
        [self getData];
        [_myCaseTablewView.mj_header endRefreshing];
    });
    
    
}


#pragma mark - TypeButtonDelegate
//实现代理
-(void)didClickTypeButtonAction:(UIButton*)button withIndex:(NSInteger)index
{
    NSLog(@"现在点击的是%ld个",index);
    self.caseStateFlag = index;
    [self.approvalData removeAllObjects];
    [self.advanceData removeAllObjects];
    [self.handlingData removeAllObjects];
    [self.closedCaseData removeAllObjects];
    [self.dataSource removeAllObjects] ;
    [self.myCaseTablewView reloadData];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self getData];

}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 160;
}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    if (_isRefresh) {
//        return 0.0000001;
//    }
//    return 65;
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyCaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCaseCell" forIndexPath:indexPath];
//    cell.titleLable.text = self.dataSource[indexPath.row][@"ay"];
//    cell.caseNumLable.text = [NSString stringWithFormat:@"案件号： %@",self.dataSource[indexPath.row][@"ah_number"]];
//    cell.bailorLable.text = [NSString stringWithFormat:@"委托人： %@",self.dataSource[indexPath.row][@"wtr"]];
//    cell.adversaryLable.text = [NSString stringWithFormat:@"对方当事人： %@",self.dataSource[indexPath.row][@"dfdsr"]];
//    
//    NSString *key = self.dataSource[indexPath.row][@"case_state"];
//    NSString *case_state = [self.case_State valueForKey:key];
//    cell.caseStatusLable.text = [NSString stringWithFormat:@"案件状态： %@",case_state];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CaseDetailViewController *CDVC = [[CaseDetailViewController alloc] init];
    CDVC.caseDetailDic = self.dataSource[indexPath.row];
    if (self.caseStateFlag == 0) {
        CDVC.caseDetailFlag = 0;
    }
    if (self.caseStateFlag == 1) {
        CDVC.caseDetailFlag = 1;
    }
    if (self.caseStateFlag == 2) {
        CDVC.caseDetailFlag = 2;
    }
    if (self.caseStateFlag == 3) {
        CDVC.caseDetailFlag = 3;
    }
    
    
    [self.navigationController pushViewController:CDVC animated:YES];
}
#pragma mark - Getter
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (NSDictionary *)case_State{
    if (!_case_State) {
        _case_State = [NSDictionary dictionaryWithObjectsAndKeys:@"终止",@"0",@"预收案",@"1",@"财务审批收案",@"2",@"主任审批收案",@"3",@"办案中",@"4",@"财务审批结案",@"5",@"主任审批结案",@"6",@"已结案",@"7", nil];
    }
    return _case_State;
}
- (UITableView *)myCaseTablewView{
    if (!_myCaseTablewView) {
        _myCaseTablewView = [[UITableView alloc] initWithFrame:CGRectMake(0, 120, SCREEN_WIDTH, SCREEN_HEIGHT - 120) style:UITableViewStylePlain];
        _myCaseTablewView.backgroundColor = [UIColor clearColor];
        _myCaseTablewView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myCaseTablewView.delegate = self;
        _myCaseTablewView.dataSource = self;
        _myCaseTablewView.estimatedRowHeight = 180;
        [_myCaseTablewView registerClass:[MyCaseCell class] forCellReuseIdentifier:@"MyCaseCell"];
        
        //下拉刷新
        
        NSMutableArray *images = [NSMutableArray array];//图片
        for (int i = 1; i < 48; i++) {//假设50张图片
            [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%zd",i]]];
        }
        MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
//        header.frame = CGRectMake(0, 0, SCREEN_WIDTH, 50);
//        [header setImages:images forState:MJRefreshStateRefreshing];// 设置正在刷新状态的动画图片
        
        // 设置普通状态的动画图片
        [header setImages:@[images[0]] forState:MJRefreshStateIdle];
        // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
        [header setImages:@[images[0]] forState:MJRefreshStatePulling];
        // 设置正在刷新状态的动画图片
        [header setImages:images forState:MJRefreshStateRefreshing];
        _myCaseTablewView.mj_header = header;// 设置header
        header.lastUpdatedTimeLabel.hidden = YES;// 隐藏时间
        header.stateLabel.hidden = YES;// 隐藏状态
        
    }
    return _myCaseTablewView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
